home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c
- Subject: Re: Wierd const problem (repost)
- Date: Fri, 22 Mar 1996 17:21:37 GMT
- Organization: Netcom
- Message-ID: <3152d883.184774812@nntp.ix.netcom.com>
- References: <827481377.25066@redifon.demon.co.uk>
- NNTP-Posting-Host: ix-dc6-04.ix.netcom.com
- X-NETCOM-Date: Fri Mar 22 11:21:18 AM CST 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- Guy Pickering <gp@redifon.demon.co.uk> wrote:
-
- > If I declare a function:
- >
- > void foo(const char* s)
- >
- > This means 's' is a pointer to a constant char. I.e. I promise not
- > to modify 's' inside the function.
- >
- > But, how do I declare a function where 's' is a pointer to a pointer
- > to a contstant char. The following doesn't work quite right:
- >
- > void foo(const char** s)
- >
- > Because when I try to pass a 'char**' info the function, the compiler
- > complains about incompatible types. Is it that the const in
- > 'const char**' doesn't refer to the 'char'? If so how can I declare
- > what I mean?
-
- You're doing it right. const char** s declares s as a pointer to
- pointer to const char. However, the language does not allow automatic
- conversion from char** to const char** because it would introduce a
- security hole. Consider:
-
- const char a[] = "abc";
- const char* b = a;
- char* d[] = { 0 };
- char** e = d;
- const char** f = e; /* illegal */
- f[0] = b;
- e[0][0] = 'x'; /* changes a[0] without a cast */
-
- If you must pass a char** where a const char** is expected, use a
- cast.
-
- Note that the above security hole would not occur if f were declared
- as
-
- const char* const* f;
-
- C++ allows conversion from char** to const char* const*, but (I assume
- due to an oversight) the C standard does not.
-
-
- Michael M Rubenstein
-